package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.SettingManager;
import com.itheima.app.manager.UserManager;
import com.itheima.domain.db.Notification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class SettingController {

    @Autowired
    SettingManager settingManager;

    @Autowired
    UserManager userManager;

    @GetMapping("/users/settings")
    public ResponseEntity findSetting(){
        return settingManager.findSetting();
    }

    @PostMapping("/users/questions")
    public ResponseEntity setQuestion(@RequestBody Map<String,String> map){
        //1.获取问题内容
        String content = map.get("content");

        //2.调用manager完成操作
        return settingManager.setQuestion(content);
    }

    @PostMapping("/users/notifications/setting")
    public ResponseEntity setNotification(@RequestBody Notification notificationParam){
        return settingManager.setNotification(notificationParam);
    }

    @GetMapping("/users/blacklist")
    public ResponseEntity findBlackListByPage(
            @RequestParam(value = "page",defaultValue = "1") int pageNum,
            @RequestParam(value = "pagesize",defaultValue = "8") int pageSize){

        return settingManager.findBlackListByPage(pageNum,pageSize);
    }

    @DeleteMapping("/users/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid") Long blackId){
        return settingManager.deleteBlackList(blackId);
    }

    //TODO 修改手机验证码
    //发送短信
    @PostMapping("/users/phone/sendVerificationCode")
    public ResponseEntity sendCode(@RequestHeader("Authorization") String token) {

        String phone = UserHolder.get().getPhone();

        return settingManager.sendCode(phone);
    }

    //短信验证
    @PostMapping("/users/phone/checkVerificationCode")
    public ResponseEntity verifyCode(
            @RequestBody Map<String,String> map,
            @RequestHeader("Authorization") String token) {
        String verificationCode = map.get("verificationCode");
        String phone = UserHolder.get().getPhone();
        return settingManager.verifyCode(phone,verificationCode);

    }

    //保存手机号
    @PostMapping("/users/phone")
    public ResponseEntity updatePhone(@RequestBody Map<String,String> map){

        String phone = map.get("phone");
        return settingManager.updatePhone(phone);
    }
}
